package com.example.zwgt.yrfk.ui.main.presenter;


import android.annotation.SuppressLint;
import android.util.Log;

import com.apkfuns.logutils.LogUtils;
import com.baidu.ocr.sdk.utils.LogUtil;
import com.blankj.utilcode.util.ToastUtils;
import com.example.zwgt.yrfk.base.mvpbase.baseImpl.BasePresenterImpl;
import com.example.zwgt.yrfk.bean.LoginUser;
import com.example.zwgt.yrfk.retroft.Api;
import com.example.zwgt.yrfk.retroft.ExceptionHelper;
import com.example.zwgt.yrfk.ui.main.activity.LoginActivity;
import com.example.zwgt.yrfk.ui.main.contact.LoginContact;
import com.example.zwgt.yrfk.utils.Util;


import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/*
 * 项目名:    BaseFrame
 * 包名       com.zhon.frame.mvp.login.presenter
 * 文件名:    LoginPresenter
 * 创建者:    ZJB
 * 创建时间:  2017/9/7 on 11:17
 * 描述:     TODO
 */
public class LoginPresenter extends BasePresenterImpl<LoginContact.view>
        implements LoginContact.presenter {
    LoginActivity loginActivity;
    public LoginPresenter(LoginContact.view view) {
        super(view);
    }
    public LoginPresenter(LoginActivity loginActivity) {
        super(loginActivity);
        this.loginActivity = loginActivity;
    }
    @SuppressLint("CheckResult")
    @Override
    public void login() {
//        String xauth = Util.getSignature("post",
//                "/app-login" ,
//                "" /*+ loginActivity.get("default_key","")*/,
//                headsMap.get("X-Device-Id"),
//                headsMap.get("X-Version"));
//        String base64 = Util.getXAuthor(xauth,loginActivity.get("default_key","da6665c14f5b9749"));
        Api.getInstance().login(view.getUserName(), view.getPsd())
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {
                    addDisposable(disposable);
                    view.showLoadingDialog();
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(storiesBeen -> {
                    view.dismissLoadingDialog();
                    //处理服务器返回的数据
                    if (storiesBeen.getStatus().equals("1")) {
                        view.getLoadData(storiesBeen.getContent());
                        //处理服务器返回的错误信息
                    } else {
                        Log.e("Throwable", storiesBeen.getMsg());
                        ToastUtils.showShort(storiesBeen.getMsg());
                    }
                    //处理网络异常
                }, throwable -> {
                    view.dismissLoadingDialog();
                    ExceptionHelper.handleException(throwable);
                });
//        addHeader("x-auth",base64);
//
//        LoginUser lu = new LoginUser(view.getUserName(), Util.getMD5(view.getPsd()));
//        Log.e("MESSAGE",headsMap.toString()+"      "+lu.toString());
//        Api.getInstance().login(headsMap, lu)
//                .subscribeOn(Schedulers.io())
//                .doOnSubscribe(disposable -> {
//                    addDisposable(disposable);
//                    view.showLoadingDialog();
//                })
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(storiesBeen -> {
//                    view.dismissLoadingDialog();
//                    //处理服务器返回的数据
//                    LogUtils.e(storiesBeen);
////                    if (storiesBeen.getStatus().equals("1")) {
//////                        view.getLoadData(storiesBeen.getContent());
////                        //处理服务器返回的错误信息
////                    } else {
//////                        Log.e("Throwable", storiesBeen.getMsg());
////                        ToastUtils.showShort(storiesBeen.getMsg());
////                    }
//                    //处理网络异常
//                }, throwable -> {
//                    view.dismissLoadingDialog();
//                    ExceptionHelper.handleException(throwable);
//                    LogUtils.e(throwable);
//                });
    }
}
